package day13;

import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

/**
 * @author joker
 * @date 2022/4/23 0023 - 1:07
 */
public class Test01 {
    public static void main(String[] args) throws IllegalAccessException, InvocationTargetException, InstantiationException, NoSuchMethodException, NoSuchFieldException {
        Class<Person> personClass = Person.class;
        //使用getDeclaredConstructor获取所有权限的构造器
        Constructor<Person> constructor = personClass.getDeclaredConstructor(int.class, String.class);
        Field age = personClass.getDeclaredField("age");
        //设置权限为public
        age.setAccessible(true);
        constructor.setAccessible(true);
        Person p = constructor.newInstance(12, "张三");
        System.out.println(p);
        //得到某个对象的age值
        System.out.println(age.get(p));
        //设置某个对象的age值
        age.set(p,13);
        System.out.println(p);
        Method m2 = personClass.getDeclaredMethod("m2", String.class);
        m2.setAccessible(true);
        Object haha = m2.invoke(p, "haha");
        System.out.println(haha);
        Method[] declaredMethods = personClass.getDeclaredMethods();
        for (Method declaredMethod : declaredMethods) {
            declaredMethod.setAccessible(true);
            System.out.println(declaredMethod.getName());
        }


    }
}
